package com.yanqu.road.server.cmd.activity.tonbingchannel.channel;

import com.yanqu.road.entity.activity.tonbingchannel.config.ChannelTonBingConfig;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ChannelTonBingProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.ServerLanguageMgr;

import com.yanqu.road.server.manger.activity.tonbingchannel.ChannelTonBingMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import io.netty.channel.ChannelHandlerContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Cmd(code = GameProtocol.S_CTON_BING_AUTO_SEND_CITY_COLLECT, desc = "")
public class TonBingMailSendCollectFromCrossCmd implements Command {

    private Logger logger = LogManager.getLogger(ChannelTonBingMgr.class.getName());

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {

        logger.debug("channel ton bing auto send collect reward begin...");

        ChannelTonBingConfig config = ChannelTonBingMgr.getConfig();
        if (config == null) {
            logger.error("channel ton bing auto send collect reward fail, no activity!");
            return;
        }

        ChannelTonBingProto.ChannelTonBingMailSendCityCollectReqMsg reqMsg = ChannelTonBingProto.ChannelTonBingMailSendCityCollectReqMsg.parseFrom(packet.getBytes());
        for (ChannelTonBingProto.ChannelTonBingCityCollectMsg msg : reqMsg.getListList()) {
            long userId = msg.getUserId();
            long count = msg.getCount();
            String reward = config.getZiZhongItem() + "=" + count;

            logger.debug("ton bing auto send collect reward : userId {}, count {}.", userId, count);

            UserInfo userInfo = UserMgr.getUserInfo(userId);
            if (userInfo == null) {
                logger.error("ton bing auto send collect reward fail, no userInfo : userId {}, count {}.", userId, count);
                continue;
            }

            String language = userInfo.getLanguage();

            String mailTitle = ServerLanguageMgr.getContent(config.getZiZhongTitle(), language);
            String mailContent = ServerLanguageMgr.getContent(config.getZiZhongContent(), language);

            MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), reward, mailContent, mailTitle, config.getActivityInfo().getActivityId(), config.getActivityInfo().getServerListStr());
        }
    }

}
